﻿@model Atomic.Permission.Entities.RoleEntity
<script type="text/javascript">
    var jqEditPanel, jqCfgPanel;
    $(function () {
        var funOperatesRelations;
        jqEditPanel = $('#roleEditPanel');
        jqCfgPanel = $('#permissionPanel');
        jqEditPanel.dialog({
            title: '新建/编辑项',
            buttons: [
                {
                    text: '保存',
                    handler: function () {
                        $('form', jqEditPanel).submit();
                    }
                }
            ]
        });
        jqCfgPanel.dialog({
            title: '权限配置-[]',
            buttons: [
                {
                    text: '保存',
                    handler: function () {
                        var arr = new Array();
                        $('input:checkbox[checked=true]', jqCfgPanel).each(function (i, d) {
                            var ss = $(d).attr('id').toString().split('_');
                            arr.push({
                                FormID: ss[1],
                                FunID: ss[2]
                            });
                        });
                        $.post('@Url.Content("~/Permission/RoleFunOperateRelationsAddOrUpdate")',
                        {
                            roleID: $('#list').datagrid('getSelected').ID,
                            data: JSON.stringify(arr)
                        },
                        function (data) {
                            if (data.Success) {
                                $.messager.alert('提示信息', '权限保存成功', 'info');
                            } else {
                                $.messager.alert('提示信息', data.Message, 'info');
                            }
                        });
                    }
                }
            ]
        });

        $('#list').datagrid({
            url: '@Url.Content("~/Permission/RolePage")',
            fit: true,
            border: false,
            nowrap: false,
            singleSelect: true,
            pagination: true,
            toolbar: [
                    {
                        text: "新建",
                        iconCls: "icon-add",
                        handler: function () {
                            $('#ID', jqEditPanel).val(0);
                            jqEditPanel.dialog('open');
                        }
                    },
                    {
                        text: "编辑",
                        iconCls: "icon-edit",
                        handler: function () {
                            var data = $('#list').datagrid('getSelected');
                            if (null == data) {
                                $.messager.alert('提示信息', '请选择要编辑的项', 'warning');
                            } else {
                                $('#ID', jqEditPanel).val(data.ID);
                                $('#Name', jqEditPanel).val(data.Name);
                                $('#Sort', jqEditPanel).val(data.Sort);
                                $('#Description', jqEditPanel).val(data.Description);
                                $('#Enabled', jqEditPanel).attr('checked', data.Enabled);
                                jqEditPanel.dialog('open');
                            }
                        }
                    },
                    {
                        text: "删除",
                        iconCls: "icon-remove",
                        handler: function () {
                            var data = $('#list').datagrid('getSelected');
                            if (null == data) {
                                $.messager.alert('提示信息', '请选择要删除的项', 'warning');
                            } else {
                                $.messager.confirm('提示信息', '确定删除选中项？', function (btn) {
                                    if (btn) {
                                        $.post('@Url.Content("~/Permission/RoleRemove")',
                                        {
                                            id: data.ID
                                        },
                                        function () {
                                            $('#list').datagrid('reload');
                                        });
                                    }
                                });
                            }
                        }
                    },
                    '-'
                    ,
                    {
                        text: "权限配置",
                        iconCls: "icon-function",
                        handler: function () {
                            var r = $('#list').datagrid('getSelected');
                            if (null == r) {
                                $.messager.alert('提示信息', '请选择一个角色', 'warning');
                            } else {

                                $('input:checkbox', $('#permissionPanel')).attr('checked', false);

                                $.post('@Url.Content("~/Permission/RoleFunOperateRelations")',
                                {
                                    roleID: r.ID
                                },
                                function (data) {
                                    funOperatesRelations = data;
                                    if (funOperatesRelations) {
                                        $.each(data, function (i, v) {
                                            $('#opr_' + v.FormID + '_' + v.FunID).attr('checked', true);
                                        });
                                    }
                                });
                                jqCfgPanel.dialog('open');
                            }
                        }
                    }
                ],
            columns: [[
                    { field: 'Sort', title: '顺序', width: 50 },
                    { field: 'ID', title: '编号', width: 50 },
                    { field: 'Name', title: '角色名', width: 200 },
                    { field: 'Enabled', title: '是否启用', width: 60,
                        formatter: function (v) {
                            if (v) {
                                return '是';
                            } else {
                                return '<span style="color:#f00;">否<span>';
                            }
                        }
                    },
                    { field: 'Description', title: '描述' }
                ]]
        });

        //权限配置Grid
        $('#funList').treegrid({
            url: '@Url.Content("~/Permission/FunPointsOperates")',
            idField: 'ID',
            treeField: 'Key',
            fit: true,
            border: false,
            nowrap: false,
            fitColumns: false,
            checkbox: true,
            columns: [[
	                { field: 'Key', title: '键值', width: 250 },
                    { field: 'Name', title: '功能名', width: 100 },
                    { field: 'Enabled', title: '基础权限', width: 80,
                        formatter: function (v, r) {
                            var html = '<div onclick="checkThis(this)" style="float:left; width:100px; line-height:20px;"><input type="checkbox" id="opr_' + r.ID + '_0" value="0" />浏览</div>';
                            return html;
                        }
                    },
                    { field: 'FunPoints', title: '操作点', width: 300,
                        formatter: function (v, r) {
                            var html = '';
                            for (var i = 0; i < v.length; i++) {
                                html += '<div onclick="checkThis(this)" style="float:left; width:100px; line-height:20px;"><input type="checkbox" id="opr_' + r.ID + '_' + v[i].ID + '" value="' + v[i].ID + '" />' + v[i].Name + '</div>';
                            }
                            return html;
                        }
                    }
                ]],
            onLoadSuccess: function () {
                //                if (funOperatesRelations) {
                //                    $.each(funOperatesRelations, function (i, v) {
                //                        $('#opr_' + v.FormID + '_' + v.FunID).attr('checked', true);
                //                    });
                //                }
            }
        });
    });
    function checkThis(obj) {
        var chk = $(':checkbox', $(obj));
        if (chk.attr('checked')) {
            chk.attr('checked', false);
        } else {
            chk.attr('checked', true);
        }
    }
    function onSuccess(data) {
        if (data.Success) {
            jqEditPanel.dialog('close');
            $('#list').datagrid('reload');
            $('input:text,textarea', jqEditPanel).val('');
        }
    }
</script>
<div id="list">
</div>
<div id="roleEditPanel">
    @using (Ajax.BeginForm("RoleAddOrUpdate", new AjaxOptions
    {
        HttpMethod = "Post",
        OnSuccess = "onSuccess"
    }))
    {
        <table class="form-tab top" style="width: 500px">
            <tr>
                <td class="left">
                    @Html.LabelFor(m => m.ID)：
                </td>
                <td>
                    @Html.TextBoxFor(m => m.ID, new { @class = "text", @disabled = true })
                    @Html.HiddenFor(m => m.ID)
                </td>
            </tr>
            <tr>
                <td class="left">
                    @Html.LabelFor(m => m.Name)：
                </td>
                <td>
                    @Html.TextBoxFor(m => m.Name, new { @class = "text" })
                    @Html.ValidationMessageFor(m => m.Name)
                </td>
            </tr>
            <tr>
                <td class="left">
                    @Html.LabelFor(m => m.Sort)：
                </td>
                <td>
                    @Html.TextBoxFor(m => m.Sort, new { @class = "text easyui-numberbox" })
                    @Html.ValidationMessageFor(m => m.Sort)
                </td>
            </tr>
            <tr>
                <td class="left">
                    @Html.LabelFor(m => m.Enabled)：
                </td>
                <td>
                    @Html.CheckBoxFor(m => m.Enabled, new { @checked = true })
                </td>
            </tr>
            <tr>
                <td class="left-top" style="vertical-align: top;">
                    @Html.LabelFor(m => m.Description)：
                </td>
                <td>
                    @Html.TextAreaFor(m => m.Description, new { @class = "textarea", @style = "width: 90%; height: 80px;" })<br />
                    @Html.ValidationMessageFor(m => m.Description)
                </td>
            </tr>
        </table>
    }
</div>
<!--权限配置面板-->
<div id="permissionPanel" style="width: 780px; height: 450px;">
    <div id="funList">
        功能列表
    </div>
</div>
